package com.app.controller;

import com.app.aop.has.HasApiLimit;
import com.app.aop.has.HasRedissonLock;
import com.app.aop.has.HasRole;
import com.app.domain.bo.LinksBo;
import com.app.domain.page.Page;
import com.app.domain.vo.LinksVo;
import com.app.framework.common.R;
import com.app.service.LinksService;
import jakarta.annotation.Resource;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

/**
 * 友情链接控制器
 */
@RestController
@RequestMapping("/api/link")
public class LinksController {

    @Resource
    private LinksService linksService;

    /**
     * 全部友链列表
     *
     * @param bo 入参
     */
    @PostMapping("/all")
    public R<Page<LinksVo>> allLinks(@RequestBody LinksBo bo) {
        return R.ok(linksService.allLinks(bo));
    }

    /**
     * 已审核友情链接列表
     *
     * @param bo 入参
     */
    @PostMapping("/list")
    public R<Page<LinksVo>> lists(@RequestBody LinksBo bo) {
        return R.ok(linksService.lists(bo));
    }

    /**
     * 友链申请
     *
     * @param bo 入参
     */
    @HasApiLimit(seconds = 10)
    @HasRedissonLock("apply_link")
    @PostMapping("/apply")
    public R<?> apply(@RequestBody LinksBo bo) {
        linksService.apply(bo);
        return R.ok(null, "申请成功,等待审核!");
    }

    /**
     * 友链审核
     *
     * @param linkId 友链ID
     */
    @HasRole()
    @HasRedissonLock("examine_link")
    @PostMapping("/examine/{linkId}")
    public R<?> examine(@PathVariable("linkId") Long linkId) {
        linksService.examine(linkId);
        return R.ok(null, "操作成功");
    }

    /**
     * 修改友链
     *
     * @param bo 入参
     */
    @HasRole()
    @HasRedissonLock("update_link")
    @PostMapping("/update")
    public R<?> updateOne(@RequestBody LinksBo bo) {
        Assert.notNull(bo.getId(), "ID不能为空");
        linksService.updateOne(bo);
        return R.ok(null, "修改成功");
    }

    /**
     * 删除友链
     *
     * @param linkId 友链ID
     */
    @HasRole()
    @HasRedissonLock("delete_link")
    @PostMapping("/delete/{linkId}")
    public R<?> deleteOne(@PathVariable("linkId") Long linkId) {
        linksService.deleteOne(linkId);
        return R.ok(null, "删除成功");
    }

    /**
     * 友链访问
     *
     * @param linkId 友链ID
     */
    @PostMapping("/look/{linkId}")
    public R<?> look(@PathVariable("linkId") Long linkId) {
        linksService.look(linkId);
        return R.ok();
    }

}